package com.young.api;

import com.young.annot.Log;
import com.young.domain.SysMenu;
import com.young.domain.SysUser;
import com.young.req.AddMenuReq;
import com.young.res.SysMenuRolePermRes;
import com.young.shop.service.SysMenuService;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 菜单，权限，角色加载接口
 * @Author: kangyang
 * @Date: 2021/5/14 11:04
 **/
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

  private static final Logger log = LoggerFactory.getLogger(SysMenuController.class);

  @Autowired
  private SysMenuService sysMenuService;

  @GetMapping("/nav")
  public ResponseEntity getMenu() {
    SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
    SysMenuRolePermRes sysMenuRolePermRes = sysMenuService.getMenuRolePerm(user.getUserId());
    return ResponseEntity.ok(sysMenuRolePermRes);
  }

  @GetMapping("/table")
  public ResponseEntity<List<SysMenu>> getAllMenu() {
    List<SysMenu> allMenu = sysMenuService.getAllMenu();
    return ResponseEntity.ok(allMenu);
  }

  @GetMapping("/list")
  @Log(name = "查询菜单列表")
  public ResponseEntity<List<SysMenu>> listMenu() {
    List<SysMenu> allMenu = sysMenuService.getAllMenu();
    return ResponseEntity.ok(allMenu);
  }

  @PostMapping
  @ApiOperation("新增菜单")
  @Log(name = "新增菜单")
  public ResponseEntity<Boolean> addMenu(@RequestBody AddMenuReq addMenuReq) {
    Boolean result = sysMenuService.inserMenu(addMenuReq);
    return ResponseEntity.ok(result);
  }

  @GetMapping("/info/{menuId}")
  @ApiOperation("根据菜单Id查询菜单信息")
  @Log(name = "根据菜单Id查询菜单信息")
  public ResponseEntity<SysMenu> getMenuInfo(@PathVariable Long menuId) {
    log.info("根据菜单Id查询菜单信息开始, menuId->{}", menuId);
    SysMenu sysMenu = sysMenuService.getMenuByMenuId(menuId);
    log.info("根据菜单Id查询菜单信息结束, menu->{}", sysMenu);
    return ResponseEntity.ok(sysMenu);
  }

  @PutMapping
  @ApiOperation("修改菜单信息")
  @Log(name = "修改菜单信息")
  public ResponseEntity<Boolean> updateMenu(@RequestBody AddMenuReq addMenuReq) {
    Boolean result = sysMenuService.updateMenuInfo(addMenuReq);
    return ResponseEntity.ok(result);
  }

  @DeleteMapping("/{menuId}")
  @ApiOperation("删除菜单")
  @Log(name = "删除菜单")
  public ResponseEntity<Boolean> deleteMenu(@PathVariable Long menuId) {
    Boolean result = sysMenuService.deleteMenu(menuId);
    return ResponseEntity.ok(result);
  }

}
